package com.crud.demo1.demos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crud.demo1.demos.entity.TableUser;
import com.crud.demo1.demos.Service.ITableUserService;
import com.crud.demo1.demos.commons.AjaxResult;
import com.crud.demo1.demos.controller.dto.UserDto;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author zhangning
 * @since 2024/4/10 14:08
 */
@Api(tags = "用户模块")
@Slf4j
@Controller
@RequestMapping("user1")
@ResponseBody
public class UserController {

    private final ITableUserService tableUserService;

    @Autowired
    public UserController(ITableUserService tableUserService) {
        this.tableUserService = tableUserService;
    }

    /**
     * 查询用户 分页模糊查询
     */
    @RequestMapping(value = "getUser", method = {RequestMethod.POST})
    public AjaxResult getUser(@RequestBody UserDto userDto) {
        if (Objects.isNull(userDto)) {
            return AjaxResult.fail("请检查请求参数！");
        }
        /*if (Objects.nonNull(userDto.getAge())) {
            LambdaQueryWrapper<TableUser> queryWrapper = new LambdaQueryWrapper<TableUser>()
                    .like(TableUser::getName, StringUtils.trim(userDto.getName()))
                    .like(TableUser::getEmail, StringUtils.trim(userDto.getEmail()))
                    .eq(TableUser::getAge, userDto.getAge());
            Page<TableUser> pageInfo = tableUserService.page(new Page<>(userDto.getCurrent(), userDto.getPageSize()),
                    queryWrapper);
            return AjaxResult.success(pageInfo);
        }
        LambdaQueryWrapper<TableUser> queryWrapper = new LambdaQueryWrapper<TableUser>()
                .like(TableUser::getName, StringUtils.trim(userDto.getName()))
                .like(TableUser::getEmail, StringUtils.trim(userDto.getEmail()));
        Page<TableUser> pageInfo = tableUserService.page(new Page<>(userDto.getCurrent(), userDto.getPageSize()),
                queryWrapper);*/
        IPage<TableUser> userIPage = tableUserService.getUser(userDto);
        return AjaxResult.ok(userIPage);
    }

    public AjaxResult getUser1(@RequestBody UserDto userDto) {
        if (ObjectUtils.isEmpty(userDto)) {
            return AjaxResult.ok("请检查请求参数!");
        }
        LambdaQueryWrapper<TableUser> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(userDto.getName())){
            queryWrapper.like(TableUser::getName, userDto.getName());
        }
        if (StringUtils.isNotBlank(userDto.getEmail())){
            queryWrapper.like(TableUser::getEmail, userDto.getEmail());
        }
        if (Objects.nonNull(userDto.getAge())){
            queryWrapper.eq(TableUser::getAge, userDto.getAge());
        }
        Page<TableUser> userPage = tableUserService.page(new Page<>(userDto.getCurrent(), userDto.getPageSize()), queryWrapper);
        if (CollectionUtils.isEmpty(userPage.getRecords())){
            return AjaxResult.ok("没有查询到数据");
        }
        return AjaxResult.ok(userPage.getRecords());
    }

    /**
     * 添加用户
     *
     * @param userDto 传输对象
     * @return AjaxResult
     */
    @PostMapping("addUser")
    public AjaxResult addUser(@RequestBody UserDto userDto) {
        if (StringUtils.isNotBlank(userDto.getName()) &&
                StringUtils.isNotBlank(userDto.getEmail()) &&
                Objects.nonNull(userDto.getAge())) {
            TableUser tableUser = new TableUser();
            BeanUtils.copyProperties(userDto, tableUser);
            tableUserService.save(tableUser);
            return AjaxResult.ok("添加成功");
        }
        log.warn("请填写用户名、年龄、邮箱！");
        return AjaxResult.fail("请填写用户名、年龄、邮箱！");
    }

    /**
     * 修改用户
     *
     * @param userDto 传输对象
     * @return AjaxResult
     */
    @PutMapping("updateUser")
    public AjaxResult updateUser(@RequestBody UserDto userDto) {
        if (StringUtils.isNotBlank(userDto.getName()) &&
                StringUtils.isNotBlank(userDto.getEmail()) &&
                Objects.nonNull(userDto.getAge())) {
            TableUser tableUser = new TableUser();
            BeanUtils.copyProperties(userDto, tableUser);
            tableUserService.updateById(tableUser);
            return AjaxResult.ok("修改成功");
        }
        return AjaxResult.fail("请填写用户名、年龄、邮箱！");
    }

    /**
     * 删除用户
     *
     * @param ids id集合
     * @return AjaxResult
     */
    @DeleteMapping("deleteUser")
    public AjaxResult deleteUser(@RequestBody List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)|| org.apache.commons.collections4.CollectionUtils.isEmpty(ids)) {
            return AjaxResult.fail("请勾选需要删除的用户!");
        }
        tableUserService.getBaseMapper().deleteBatchIds(ids);
        return AjaxResult.ok("删除成功");
    }
}